package com.qcxy.gzy.activity.v4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;
import android.widget.ImageView;

import com.qcxy.gzy.activity.ExerciseActivity;
import com.qcxy.gzy.activity.R;
import com.qcxy.gzy.activity.base.BaseSwitchActivity;
import com.qxcy.gzy.util.PreferencesUtils;
import com.qxcy.gzy.util.SerialPortUtils;

public class InitSettingActivity extends SwitchEMActivity
        implements BaseSwitchActivity.SwitchEMListener {
    private ImageView item_btn_refresh;

    private int switchNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_v2_init_setting);
        item_btn_refresh = findViewById(R.id.item_btn_refresh);
        item_btn_refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //当前电机是否运行
                isEMRunning = false;
                PreferencesUtils.put(InitSettingActivity.this, SerialPortUtils.EM_STATUS, false);
                //设置当前运行电机类型
                runEMType = SerialPortUtils.ElectricMachineryBig;
                PreferencesUtils.put(InitSettingActivity.this, SerialPortUtils.CURRENT_EM, runEMType);
                //初始化设置电机状态
                updateEM(true, SerialPortUtils.EM_STATUS_STOP);
                updateEM(false, SerialPortUtils.EM_STATUS_STOP);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showDialog(loading);
                        switchToEMSmall();
                    }
                });
            }
        });
        item_btn_refresh.performClick();
    }

    @Override
    public void onSwitch(final boolean isSuccess, final String message, final int type) {
        if (!isAlive) return;
        switchNum++;
        hideLoadingView();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isSuccess) {
                    switchNum = 0;
                    item_btn_refresh.setVisibility(View.GONE);
                    //暂存电机信息
                    PreferencesUtils.put(InitSettingActivity.this, SerialPortUtils.CURRENT_EM, type);
                    //跳转至正常逻辑主页
                    startActivity(new Intent(InitSettingActivity.this, ExerciseActivity.class),
                            ActivityOptionsCompat.makeCustomAnimation(
                                    InitSettingActivity.this, R.anim.fade, R.anim.hold).toBundle());
                    finish();
                } else {
                    if (switchNum < 3) {
                        switchToEMSmall();
                    } else {
                        switchNum = 0;
                        SerialPortUtils.showSerialDialog(InitSettingActivity.this, "提示", message);
                    }
                }
            }
        });
        builder.append("切换结果：").append(isSuccess ? "成功" : "失败").append("\n");
        if (!isSuccess) {
            builder.append("切换失败信息").append(message).append("\n");
        }
        builder.append("------------------------------------------------------------------------\n");
        //SerialPortUtils.saveLocalDailyLog(builder.toString());
    }
}
